{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 字典的初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 4, 'b': 3, '3': 'b'}\n",
      "{'a': 5, 'b': 1, 'c': 3}\n",
      "{'a': 1, 1: 2, 3: 'b'}\n",
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "[('a', 1), ('b', 2), ('c', 3)]\n"
     ]
    }
   ],
   "source": [
    "a = dict([('a',4),('b',3),('3','b')])\n",
    "print(a)\n",
    "\n",
    "b = dict(zip('abc', [5, 1, 3]))\n",
    "print(b)\n",
    "\n",
    "c = {'a':1, 1:2, 3:'b'}\n",
    "print(c)\n",
    "\n",
    "# d = dict(a = 1,d='f', c = 2)\n",
    "# print(d)\n",
    "\n",
    "t1 = [1,2,3]\n",
    "t2 = ['a','b','c']\n",
    "dictionary = dict(zip(t2,t1))\n",
    "print(dictionary)\n",
    "print(list(zip(t2,t1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 1: 2, 3: 'b'}\n",
      "{'a': 'b', 1: 2, 3: 'c'}\n",
      "{'a': 1, 'b': 3}\n",
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "{'a': 2, 1: 'c', 'b': 3}\n",
      "{0: 'a', 1: 'b', 2: 'c', 3: 'd'}\n",
      "dict_items([('a', 0), ('b', 1), ('c', 2), ('d', 3)])\n",
      "<class 'dict_items'>\n",
      "{4: 'd', 3: 'c', 2: 'b', 1: 'a'}\n"
     ]
    }
   ],
   "source": [
    "a = {'a':1, 1:2, 3:'b'}\n",
    "print(a)\n",
    "b  = dict([('a', 'b'),(1,2),(3,'c')])\n",
    "print(b)\n",
    "c = dict(a = 1, b = 3)\n",
    "print(c)\n",
    "d = dict(zip('abc', [1,2,3]))\n",
    "print(d)\n",
    "e = dict(zip(['a', 1, 'b'], [2, 'c', 3]))\n",
    "print(e)\n",
    "\n",
    "l = dict((k, v) for k,v in enumerate(['a','b','c','d']))\n",
    "print(l)\n",
    "print(dict((v,k) for k,v in l.items()).items())\n",
    "l = {4:'d',3:'c',2:'b',1:'a'}\n",
    "print(type(l.items()))\n",
    "print(dict((k,v) for k,v in l.items()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.pandas读入字典数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'a', 2: 'b', 3: 'c', 4: 'd'}\n",
      "        a  b  c  d\n",
      "number  1  2  3  4\n",
      "   id\n",
      "a   1\n",
      "b   2\n",
      "c   3\n",
      "d   4\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "voc = {1:'a', 2:'b', 3:'c', 4:'d'}\n",
    "print(voc)\n",
    "voc_reverse = dict((v,k) for k,v in voc.items())  #为什么网页上的是按顺序的\n",
    "# print(help(pd.DataFrame.from_dict))\n",
    "word = pd.DataFrame(voc_reverse, index=['voc'])\n",
    "word.index = ['number']\n",
    "print(word)\n",
    "\n",
    "word2 = pd.DataFrame.from_dict(voc_reverse, orient='index')\n",
    "word2.columns = ['id']\n",
    "print(word2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 生成器的使用 ---生成值的序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考[解释 yield 和 Generators（生成器）](http://python.jobbole.com/87613/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1 简单的生成器理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object <genexpr> at 0x0000014D831531A8>\n",
      "21\n",
      "[] \n",
      "\n",
      "[0, 1, 2, 3, 4, 5, 6]\n",
      "21\n",
      "[0, 1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "a = (s for s in range(7))  # 这里生成的是一个生成器\n",
    "b = [s for s in range(7)]\n",
    "print(a)\n",
    "print(sum(a))          #迭代器已经使用过数据\n",
    "print(list(a), '\\n')   #所以这里输出一个空的列表\n",
    "print(b)\n",
    "print(sum(b))\n",
    "print(list(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# while循环是用来确保生成器函数永远也不会执行到函数末尾，虽然每次迭代器到结束后，可以再次创建新的迭代器，但是该方法太愚蠢了\n",
    "def gen_number():\n",
    "    while True:    \n",
    "        for i in range(10):\n",
    "            yield i\n",
    "gen = gen_number() # 该语句产生一个生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(gen)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 生成器例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 找出输入列表中的素数\n",
    "import math\n",
    "def is_prime(number):      \n",
    "    # 判断是否是质数，素数；定义：指在大於1的自然数中，除了1和該数自身外，無法被其他自然数整除的数\n",
    "    if number > 1:\n",
    "        if number == 2:      # 2是最小的质数\n",
    "            return True\n",
    "        if number % 2 == 0:  # 偶数都是合数\n",
    "            return False\n",
    "        for current in range(3, int(math.sqrt(number) + 1), 2):  # 在奇数中寻找质数\n",
    "            # print(current)\n",
    "            if number % current == 0: \n",
    "                return False\n",
    "        return True\n",
    "    return False\n",
    "def get_primes1(input_list):\n",
    "    # 找到质数并放到新的列表中\n",
    "    result_list = list()\n",
    "    for element in input_list:\n",
    "        if is_prime(element):\n",
    "            result_list.append(element)\n",
    " \n",
    "    return result_list\n",
    " \n",
    "def get_primes2(input_list):\n",
    "    return [element for element in input_list if is_prime(element)]\n",
    "\n",
    "def get_primes3(number):  # 通过生成器可以处理无穷的序列\n",
    "    while True:\n",
    "        if is_prime(number):\n",
    "            yield number    \n",
    "        number += 1      # 逐渐累加直到找到下一个素数然后返回\n",
    "        print(number)\n",
    "gen = get_primes3(11)    # 生成器只需进行一次初始化，然后使用next()就可以逐渐迭代了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object get_primes3 at 0x00000231612E4CA8>\n",
      "44\n",
      "45\n",
      "46\n",
      "47\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "47"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(gen)  # 迭代器的起始地址，不会变\n",
    "next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------ 11 -----------\n",
      "the 0-th output\n",
      "**************** 1\n",
      "------------ 2 -----------\n",
      "2\n",
      "the 1-th output\n",
      "**************** 10\n",
      "------------ 11 -----------\n",
      "11\n",
      "the 2-th output\n",
      "**************** 100\n",
      "------------ 101 -----------\n",
      "101\n"
     ]
    }
   ],
   "source": [
    "# 找到比一个数的等比级数大的最小素数\n",
    "def get_primes(number):\n",
    "    while True:\n",
    "        if is_prime(number):\n",
    "            print('------------',number,'-----------')\n",
    "            number = yield number\n",
    "            print('****************', number)\n",
    "        number += 1\n",
    "def print_successive_primes(iterations, base=10):\n",
    "    prime_generator = get_primes(base)\n",
    "    prime_generator.send(None)\n",
    "    for power in range(iterations):\n",
    "        print('the {}-th output'.format(str(power)))\n",
    "        print(prime_generator.send(base ** power)) # 把参数值传到yield赋值地方，number = yield number将参数赋给第一个number\n",
    "gen = print_successive_primes(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Producing...\n",
      "Produced [6, 0, 1]\n",
      "The running average is 2.3333333333333335\n",
      "7 3\n",
      "Producing...\n",
      "Produced [1, 8, 2]\n",
      "The running average is 3.0\n",
      "18 6\n",
      "Producing...\n",
      "Produced [5, 0, 2]\n",
      "The running average is 2.7777777777777777\n",
      "25 9\n",
      "Producing...\n",
      "Produced [7, 3, 0]\n",
      "The running average is 2.9166666666666665\n",
      "35 12\n",
      "Producing...\n",
      "Produced [4, 0, 7]\n",
      "The running average is 3.066666666666667\n",
      "46 15\n",
      "Producing...\n",
      "Produced [0, 3, 7]\n",
      "The running average is 3.111111111111111\n",
      "56 18\n",
      "Producing...\n",
      "Produced [2, 3, 4]\n",
      "The running average is 3.0952380952380953\n",
      "65 21\n",
      "Producing...\n",
      "Produced [5, 6, 9]\n",
      "The running average is 3.5416666666666665\n",
      "85 24\n",
      "Producing...\n",
      "Produced [6, 7, 9]\n",
      "The running average is 3.962962962962963\n",
      "107 27\n",
      "Producing...\n",
      "Produced [3, 8, 6]\n",
      "The running average is 4.133333333333334\n",
      "124 30\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    " \n",
    "def get_data():\n",
    "    \"\"\"返回0到9之间的3个随机数\"\"\"\n",
    "    return random.sample(range(10), 3)\n",
    " \n",
    "def consume():\n",
    "    \"\"\"显示每次传入的整数列表的动态平均值\"\"\"\n",
    "    running_sum = 0\n",
    "    data_items_seen = 0\n",
    " \n",
    "    while True:\n",
    "        data = yield\n",
    "        data_items_seen += len(data)\n",
    "        running_sum += sum(data)\n",
    "        print('The running average is {}'.format(running_sum / float(data_items_seen)))\n",
    "        print(running_sum, data_items_seen)\n",
    " \n",
    "def produce(consumer):\n",
    "    \"\"\"产生序列集合，传递给消费函数（consumer）\"\"\"\n",
    "    while True:\n",
    "        data = get_data()\n",
    "        print('Produced {}'.format(data))\n",
    "        consumer.send(data)\n",
    "        yield             # 循环一次先保存状态并暂时退出，否则会出现死循环，yield本身不会返回值\n",
    " \n",
    "if __name__ == '__main__':\n",
    "    consumer = consume()\n",
    "    consumer.send(None)   # 当用send启动生成器，第一次执行到yield时要发送空的值\n",
    "    producer = produce(consumer)\n",
    " \n",
    "    for _ in range(10):\n",
    "        print('Producing...')\n",
    "        next(producer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= max([1,2,3,1,4,2])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    3\n",
       "4    2\n",
       "5    6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "a = pd.Series([1,2,3,3,2,6])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "<class 'numpy.ndarray'>\n",
      "(3, 3) \n",
      "\n",
      "[[[1 2 3]\n",
      "  [4 5 6]\n",
      "  [7 8 9]]]\n",
      "<class 'numpy.ndarray'>\n",
      "(1, 3, 3)\n",
      "\n",
      "c[0]= [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "c[0][1]= [7 8 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3,4,5,6,7,8,9])\n",
    "b = a.reshape([3,3])\n",
    "print(b)\n",
    "print(type(b))\n",
    "print(b.shape, '\\n')\n",
    "c = a.reshape([1,3,-1])\n",
    "print(c)\n",
    "print(type(c))\n",
    "print(c.shape)\n",
    "print()\n",
    "\n",
    "print('c[0]=', c[0])\n",
    "print('c[0][1]=', c[0][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]\n",
      " [3]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "print(a.reshape([3,1]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
